home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr36 / putt100.zip / HMB2JAM.ZIP / JAMSUB.C < prev    next >
C/C++ Source or Header  |  1993-07-01  |  4KB  |  213 lines

  1. /*
  2. **  JAM(mbp) - The Joaquim-Andrew-Mats Message Base Proposal
  3. **
  4. **  HMB to JAM converter
  5. **
  6. **  Written by Mats Wallin
  7. **
  8. **  ----------------------------------------------------------------------
  9. **
  10. **  jamsub.c (JAMmb)
  11. **
  12. **  Common JAM routines used for the HMB2JAM program
  13. **
  14. **  Copyright 1993 Joaquim Homrighausen, Andrew Milner, Mats Birch, and
  15. **  Mats Wallin. ALL RIGHTS RESERVED.
  16. **
  17. **  93-06-28    MW
  18. **  Initial coding.
  19. */
  20.  
  21. #include <dos.h>
  22. #include <io.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <time.h>
  27.  
  28. #include "jammb.h"
  29.  
  30. #include "jamsub.h"
  31.  
  32.  
  33. /* ---------------------------------------------------------------------- *
  34.  *
  35.  *  JamMsgInit
  36.  *
  37.  *    Initiates the structures used for a message
  38.  *
  39.  * ---------------------------------------------------------------------- */
  40.  
  41. int JamMsgInit( JAMAPIREC * pJam )
  42. {
  43.   memset( &pJam->Idx, '\0', sizeof( JAMIDXREC ));
  44.   pJam->Idx.UserCRC = ( UINT32 ) -1L;
  45.  
  46.   memset( &pJam->Hdr, '\0', sizeof( JAMHDR ));
  47.  
  48.   pJam->Hdr.MsgIdCRC    = ( UINT32 ) -1L;
  49.   pJam->Hdr.ReplyCRC    = ( UINT32 ) -1L;
  50.   pJam->Hdr.PasswordCRC = ( UINT32 ) -1L;
  51.  
  52.   return( 1 );
  53. }
  54.  
  55.  
  56. /* ---------------------------------------------------------------------- *
  57.  *
  58.  *  JamMsgDeinit
  59.  *
  60.  * ---------------------------------------------------------------------- */
  61.  
  62. int JamMsgDeinit( JAMAPIREC * pJam )
  63. {
  64.   memset( &pJam->Hdr, '\0', sizeof( JAMHDR ));
  65.  
  66.   return( 1 );
  67. }
  68.  
  69.  
  70. /* ---------------------------------------------------------------------- *
  71.  *
  72.  *  JamMsgAddSFldStr
  73.  *
  74.  * ---------------------------------------------------------------------- */
  75.  
  76. int JamMsgAddSFldStr( JAMAPIREC * pJam, UINT16 SubFld, CHAR8 * Str, UINT32 * pSubFldPos )
  77. {
  78.   int           Len         = strlen( Str );
  79.  
  80.   if( !JAMmbAddField( pJam, SubFld, 1, strlen( Str ), pSubFldPos, Str ))
  81.     puts( "WARNING: Work buffer for subfields to small" );
  82.  
  83.   switch( SubFld )
  84.     {
  85.     case JAMSFLD_RECVRNAME :
  86.       strlwr( Str );
  87.       pJam->Idx.UserCRC = JAMsysCrc32( Str, Len, ( UINT32 ) -1L );
  88.       break;
  89.  
  90.     case JAMSFLD_MSGID :
  91.       strlwr( Str );
  92.       pJam->Hdr.MsgIdCRC = JAMsysCrc32( Str, Len, ( UINT32 ) -1L );
  93.       break;
  94.  
  95.     case JAMSFLD_REPLYID :
  96.       strlwr( Str );
  97.       pJam->Hdr.ReplyCRC = JAMsysCrc32( Str, Len, ( UINT32 ) -1L );
  98.       break;
  99.     }
  100.  
  101.   return( 1 );
  102. }
  103.  
  104.  
  105. /* ---------------------------------------------------------------------- *
  106.  *
  107.  *  JamMsgWrite
  108.  *
  109.  * ---------------------------------------------------------------------- */
  110.  
  111. int JamMsgWrite( JAMAPIREC * pJam, CHAR8 * pMsgTxt )
  112. {
  113.   int     LockTryCnt = 0;
  114.   UINT32  MsgNo;
  115.  
  116. /*
  117. **  Lock the messagebase
  118. */
  119.  
  120.   while( !JAMmbLockMsgBase( pJam, 1 ))
  121.     {
  122.     if( ++LockTryCnt >= 15 )
  123.       {
  124.       puts( "Unable to get lock on messagebase" );
  125.       exit( 3 );
  126.       }
  127.  
  128. #if defined(_MSC_VER) || defined(_QC)
  129. #else
  130.     sleep( 1 );      /* Wait one second */
  131. #endif
  132.     }
  133.  
  134.  
  135. /*
  136. **  Get the message number for the new message
  137. */
  138.  
  139.   MsgNo = ( filelength( pJam->IdxHandle ) / sizeof( JAMIDXREC )) + pJam->HdrInfo.BaseMsgNum;
  140.   pJam->Hdr.MsgNum = MsgNo;
  141.  
  142.  
  143. /*
  144. **  Get the offset in the header file for the next message header
  145. */
  146.  
  147.   pJam->Idx.HdrOffset = filelength( pJam->HdrHandle );
  148.  
  149.  
  150. /*
  151. **  And get the offset in the text file for the next text
  152. */
  153.  
  154.   pJam->Hdr.TxtOffset = filelength( pJam->TxtHandle );
  155.  
  156.  
  157. /*
  158. **  Store the index record
  159. */
  160.  
  161.   if( !JAMmbStoreMsgIdx( pJam, MsgNo ))
  162.     {
  163.     printf( "Error writing JAMIDXREC: %d\n", pJam->APImsg );
  164.     exit( 4 );
  165.     }
  166.  
  167.  
  168. /*
  169. **  And the header record
  170. */
  171.  
  172.   if( !JAMmbStoreMsgHdr( pJam, MsgNo ))
  173.     {
  174.     printf( "Error writing JAMHDR: %d\n", pJam->APImsg );
  175.     exit( 4 );
  176.     }
  177.  
  178.  
  179. /*
  180. **  Write all the subfields
  181. */
  182.  
  183.   if( JAMsysWrite( NULL, pJam->HdrHandle, pJam->WorkBuf, ( int ) pJam->Hdr.SubfieldLen ) != ( int ) pJam->Hdr.SubfieldLen )
  184.     {
  185.     printf( "Error writing SubFields\n" );
  186.     exit( 4 );
  187.     }
  188.  
  189.  
  190. /*
  191. **  Write the message text
  192. */
  193.  
  194.   if( !JAMmbStoreMsgTxtBuf( pJam, pMsgTxt, pJam->Hdr.TxtLen, 1 ))
  195.     {
  196.     printf( "Error writing message text: %d\n", pJam->APImsg );
  197.     exit( 4 );
  198.     }
  199.  
  200.  
  201. /*
  202. **  Unlock the messagebase
  203. */
  204.  
  205.   pJam->HdrInfo.ActiveMsgs++;
  206.   JAMmbUnLockMsgBase( pJam, 1 );
  207.  
  208.   return( 1 );
  209. }
  210.  
  211.  
  212. /* end of file "jamsub.c" */
  213.